package com.shengde.jinwawaforleader.model.homePage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.shengde.jinwawaforleader.HomeApplication;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.model.homePage.adapter.LocalAlbumsAdapter;
import com.shengde.jinwawaforleader.model.homePage.studentproduciton.FileTraversal;
import com.shengde.jinwawaforleader.model.homePage.studentproduciton.LaocalImagesActivity;
import com.shengde.jinwawaforleader.utils.MessageEvent;
import com.shengde.jinwawaforleader.utils.Util;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LaocalAlbumsActivity extends Activity implements OnItemClickListener {

	ListView listview;
	Util util;
	LocalAlbumsAdapter listAdapter;
	List<FileTraversal> localFileList;
	private TextView tv_back, tv_title, fill_add;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.local_albums);
		//绑定事件接受
		EventBus.getDefault().register(this);

		/**
		 * 这个是B
		 * */
//		HomeApplication.activityS.add(this);
		fbi();
		initdata();
		listener();
	}

	private void fbi() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		fill_add = (TextView) findViewById(R.id.fill_add);
		listview = (ListView) findViewById(R.id.listView1);
	}

	private void initdata() {
		tv_title.setText("选择相册");
		fill_add.setVisibility(View.INVISIBLE);
		util = new Util(this);
		localFileList = util.LocalImgFileList();
		List<HashMap<String, String>> listdata = new ArrayList<HashMap<String, String>>();
		if (localFileList != null && localFileList.size() > 0) {
			for (int i = 0; i < localFileList.size(); i++) {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("filecount", localFileList.get(i).subList.size() + "张");
				map.put("imgpath",
						localFileList.get(i).subList.get(0) == null ? null : (localFileList.get(i).subList.get(0)));
				map.put("filename", localFileList.get(i).filename);
				listdata.add(map);
			}
		} else {
			Toast.makeText(this, "找不到图片", Toast.LENGTH_SHORT).show();
			this.finish();
		}
		listAdapter = new LocalAlbumsAdapter(this, listdata);
		listview.setAdapter(listAdapter);
		listview.setOnItemClickListener(this);
	}

	private void listener() {
		tv_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Subscribe
	public void onMessageEvent(MessageEvent event) {
		if (event.message.equals(MessageEvent.SELECT_LOCAL_PHOTO_OK)) {
			finish();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		Intent intent = new Intent(this, LaocalImagesActivity.class);
		Bundle bundle = new Bundle();
		bundle.putParcelable("data", localFileList.get(arg2));
		System.out.println("LUOC" + localFileList.get(arg2).toString());
		intent.putExtras(bundle);
		startActivity(intent);

	}

	@Override
	public void onStop() {
		// 当进程不可见的时候，挂起不需要的剩下的UI更新、线程或者处理，保存所有的编辑或者
		// 状态改变，因为这个进程可能会被销毁(从而为其他进程释资源)
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);

	}
}